<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * Model CLASS - DB Model - OS Admin
 * 
 * @name      DB Model
 * @package		OS CRM
 * @category  Class
 * @subcategory  Model
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------

class DB_Model extends Model {

  //set db - listing data
  var $e_list_limit     = '10';
  var $e_list_offset    = '0';
  var $e_list_order     = 'asc';
  var $e_list_order_by  = 'os_articles_id';
  var $e_list_id        = 'os_articles_id';
  
  var $e_jid            = '0';
  var $e_vid            = '0';
  
  
  function __construct() {
    // Call the Model constructor
    parent::__construct();
  }

  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get List
	 *
	 * @descp  this will get the list of data for the table selected
	 * @access public
	 * @return void
	*/
  function bk_get_list() {
    $return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
		$qry = "SELECT  * FROM ".TBL_ARTICLES." WHERE os_journals_id = '".$this->e_jid."' 
            ORDER BY  $order_by	$order LIMIT $offset  , $limit";			
		$return_arr = run_cust_qry($qry);
		return $return_arr;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get List
	 *
	 * @descp  this will get the list of data for the table selected
	 * @access public
	 * @return void
	*/
  function bk_get_list_cnt() {
    $return_arr  = array();
    $qry = "SELECT COUNT(".$this->e_list_id.") as cnt FROM ".TBL_ARTICLES." 
            WHERE 
            os_journals_id = '".$this->e_jid."'
            AND os_volume_id = '".$this->e_vid."'  ";		
    
    $return_arr = run_cust_qry($qry,TRUE);
		return $return_arr;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend  Seacrh - Get List
	 *
	 * @descp  this will get the list of data for the table selected
	 * @access public
   * @param  boolean 
	 * @return void
	*/
  function bk_get_srch_list($cond = '', $cnt = FALSE) {
    $return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
    
    $flds = ' * ';
    if( $cnt == TRUE ){
      $flds = ' COUNT('.$this->e_list_id.') as cnt ';
    }
    
		$qry = "SELECT $flds FROM ".TBL_ARTICLES." 
            WHERE  
            os_journals_id = '".$this->e_jid."'
            AND os_volume_id = '".$this->e_vid."' 
            AND $cond ";			
    
    if( $cnt == FALSE ){
      $qry .= " ORDER BY  $order_by	$order LIMIT $offset  , $limit ";
    }
    //__debug($qry);
		$return_arr = run_cust_qry($qry,$cnt);
		return $return_arr;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Get Data
	 *
	 * @descp  get the data for edit view
	 * @access public
	 * @param  integer - id
	 * @return void
	*/  
  function bk_get_data($id = 0){
    $return_arr  = array();
    $qry = "SELECT  * 
            FROM " . TBL_ARTICLES . "
            WHERE os_articles_id = '" . $id . "'
          ";
    //__debug($qry);exit;
    $handle = $this->db->query($qry);
    $num_rows = $handle->num_rows();
    if ($num_rows > 0) {
      $return_arr = $handle->row_array(); 
      return $return_arr;
    }  else {
      return FALSE;
    }  
    
  }
  
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Insert
	 *
	 * @descp  Insert data in current table
	 * @access public
	 * @return integer - Id
	*/ 
  function bk_insert(){
    $id = 0;
    $data = array();      
    $status = $this->input->post('os_volume_status');
    $status = (!empty($status)) ? $status:0;     
    $data['os_volume_status']         = $status;
    $data['os_journals_id']           = $this->e_jid;
    $data['os_volume_id']             = $this->e_vid;
    $data['os_volume_year']           = $this->input->post('os_volume_year');
    $data['os_volume_no']             = $this->input->post('os_volume_no');
    $data['os_volume_issue_no']       = $this->input->post('os_volume_issue_no');    
    $data['os_volume_issue_dt_range'] = $this->input->post('os_volume_issue_dt_range');
    $data['os_volume_price']          = $this->input->post('os_volume_price');
    $data['os_volume_pages']          = $this->input->post('os_volume_pages');    
    $data['os_volume_abtract']        = filter_data($this->input->post('os_volume_abtract'));
    $data['os_volume_content']        = filter_data($this->input->post('os_volume_content'));
    $data['os_volume_created']        = date(DB_DATE_FORMAT);
    $this->db->insert(TBL_ARTICLES, $data); 
    $id = $this->db->insert_id();
    return $id;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Update
	 *
	 * @descp  Update data in current table
	 * @access public
	 * @param  integer - id
	 * @return void
	*/ 
  function bk_update($id = 0){   
    
    $data = array(); 
    $status = $this->input->post('os_volume_status');
    $status = (!empty($status)) ? $status:'0';     
    $data['os_volume_status']         = $status;
    $data['os_volume_year']           = $this->input->post('os_volume_year');
    $data['os_volume_no']             = $this->input->post('os_volume_no');
    $data['os_volume_issue_no']       = $this->input->post('os_volume_issue_no');    
    $data['os_volume_issue_dt_range'] = $this->input->post('os_volume_issue_dt_range');
    $data['os_volume_price']          = $this->input->post('os_volume_price');
    $data['os_volume_pages']          = $this->input->post('os_volume_pages');    
    $data['os_volume_abtract']        = filter_data($this->input->post('os_volume_abtract'));
    $data['os_volume_content']        = filter_data($this->input->post('os_volume_content'));
    $data['os_volume_modified']       = date(DB_DATE_FORMAT);
    
    $this->db->where('os_articles_id', $id);
    $this->db->update(TBL_ARTICLES, $data); 
    
    return $id;
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Update Mapping Info
	 *
	 * @descp  Update mapping data related to current table
	 * @access public
	 * @param  integer - id
	 * @return void
	*/ 
 function bk_update_mapping_info($last_id = 0) {

    /* update mapping info */
    if (!empty($last_id)) {
      
      /*update category info*/
      $os_category_id = $this->input->post('os_category_id');
      if (is_array($os_category_id) && count($os_category_id) > 0) {
        $del = "DELETE FROM " . TBL_JOR_MAP_CAT . " WHERE os_category_id IN (" . implode(', ', $os_category_id) . ") AND os_journals_id = '".$last_id."' ";
        exec_cust_qry($del);

        foreach ($os_category_id as $value) {
          $qry = "INSERT INTO " . TBL_JOR_MAP_CAT . " VALUES 
                ( NULL, " . $last_id . ", " . $value . ") ";
          exec_cust_qry($qry);
        }
      }

      /*update editors info*/
      $os_editor_id = $this->input->post('os_editor_id');
      if (is_array($os_editor_id) && count($os_editor_id) > 0) {
        $del = "DELETE FROM " . TBL_JOR_MAP_EDITOR . " WHERE os_editor_id IN (" . implode(', ', $os_editor_id) . ") AND os_journals_id = '".$last_id."' ";
        exec_cust_qry($del);

        foreach ($os_editor_id as $value) {
          $qry = "INSERT INTO " . TBL_JOR_MAP_EDITOR . " VALUES 
                ( NULL, " . $last_id . ", " . $value . ") ";
          exec_cust_qry($qry);
        }
      }

      /*update publishers info*/
      $os_pub_id = $this->input->post('os_pub_id');
      if (is_array($os_pub_id) && count($os_pub_id) > 0) {
        $del = "DELETE FROM " . TBL_JOR_MAP_PUB . " WHERE os_pub_id IN (" . implode(', ', $os_pub_id) . ") AND os_journals_id = '".$last_id."' ";
        exec_cust_qry($del);

        foreach ($os_pub_id as $value) {
          $qry = "INSERT INTO " . TBL_JOR_MAP_PUB . " VALUES 
                ( NULL, " . $last_id . ", " . $value . ") ";
          exec_cust_qry($qry);
        }
      }
    }
  }
  
  function bk_upload_file($last_id = 0) {
    $uploaddir = ROOTBASEPATH . MEDIA_JOURNALS . $last_id . '/';
    $file_state = (isset($_FILES['os_journals_cover_img']['name']) && !empty ($_FILES['os_journals_cover_img']['name'])) ? TRUE:FALSE;
    if ($file_state && !empty($last_id)) {

      if(!is_dir($uploaddir)){
        mkdir($uploaddir);
      }

      $filename = time() . '_' . $last_id . '_' . url_title(basename($_FILES['os_journals_cover_img']['name']), '_');
      $uploadfile = $uploaddir . $filename;
      //echo $uploadfile;
      $status = __upload_file($uploadfile, 'os_journals_cover_img');

      /*unlink file*/
      $hd_os_journals_cover_img = $this->input->post('hd_os_journals_cover_img');
      $del_file = $uploaddir . $hd_os_journals_cover_img;
      if(is_file($del_file)){
        unlink($del_file);
      }

      $data_up = array(
          'os_journals_cover_img' => $filename,
          'os_journals_modified' => date(DB_DATE_FORMAT)
      );
      $this->db->where('os_journals_id', $last_id);
      $this->db->update(TBL_ARTICLES, $data_up);
    }
  }
  
  // ------------------------------------------------------------------------
  
  /** 
	 * Backend - Update
	 *
	 * @descp  Update data in current table
	 * @access public
	 * @param  integer - id
	 * @return void
	*/ 
  function bk_update_status($id = 0,$status){   
    
    $data = array(); 
    $status = (!empty($status)) ? '1':'0';     
    $data['os_articles_status'] = $status;    
    $data['os_articles_modified'] = date(DB_DATE_FORMAT);
    $this->db->where('os_articles_id', $id);
    $this->db->update(TBL_ARTICLES, $data);
    return $id;
  }
}
?>
